clear; clc;

%% Generate the Baseline Results
FileName    =   'Result_Baseline';
StdVec      =   [1 1 1 -1 -1 60 -10]'*0.01/4;
PP          =   Setup_PP(struct('KAPPA',0.00/4,'XI',1/82.5/4,'Pir_SS',0.02/4,...
                                'Flag_dE',0,'Flag_Price',0,...
                                'B_Total_SS',0.91,'b_lb',-0.29,...
                                'TrProb_H',0.98,'Prob_H',0.37,...
                                'TrProb_ext',0.92,'Prob_ext',0.33,...
                                'Fin_DomShare',0.79,'Fin_AdjCost',0.8,...
                                'Cons_FracH',0.60,...
                                'RHO_M_dom',0.68,'RHO_M_ext',0.81,'RHO_Y_H',0.5,'RHO_p_F',0.90,...
                                'Taylor_Pi',1.1,'Taylor_ir',0.87,...
                                'Cons_ElasTN',6.19,'Cons_ElasHF',6.19));
Solver_Main(PP,StdVec,'FileName',FileName);
%% Different Results
load([FileName,'.mat']);
IRF             =   TempFun_AugIRF(PP,SS,MODEL,EquJac,IRF);

% Baseline
[IRF_Main,SOLUTION_Main] ...
                =   TempFun_NewIRF(PP,SS,MODEL,EquJac,StdVec);
List_Taylor_dE  =   [0,10];
List_Elas       =   linspace(0.1,1,10)'*6.19;

IRFs            =   cell(length(List_Taylor_dE),length(List_Elas));
SOLUTIONs       =   cell(length(List_Taylor_dE),length(List_Elas));
for ii=1:length(List_Taylor_dE)
    for jj=1:length(List_Elas)
        TempPP              =   PP;
        TempPP.Taylor_dE    =   List_Taylor_dE(ii);
        TempPP.Cons_ElasTN  =   List_Elas(jj);
        TempPP.Cons_ElasHF  =   List_Elas(jj);
        
        [IRFs{ii,jj},SOLUTIONs{ii,jj}] ...
                            =   TempFun_NewIRF(TempPP,SS,MODEL,EquJac,StdVec);
    end
end
save('SOLUTIONs.mat','List_Taylor_dE','List_Elas','SOLUTIONs');

%% Collect results
TempDict        =   struct('Flexible_Low', [1,1], 'Fixed_Low', [2,1],...
                           'Flexible_High', [1,10], 'Fixed_High', [2,10]);
TempList        =   fieldnames(TempDict);
for ii=1:length(TempList)
    vv          =   TempList{ii};
    Step_2_Decomposition(SOLUTIONs{TempDict.(vv)(1),TempDict.(vv)(2)},['TableGraphs/',vv,'/']);
    SubFun_CollectResults(SOLUTIONs{TempDict.(vv)(1),TempDict.(vv)(2)},['TableGraphs/',vv,'/']);
end

NN              =   length(List_Elas);
KeyStat         =   struct('AggC',zeros(2,NN),'StdC',zeros(2,NN),'DiffC',zeros(2,NN));
for ii=1:NN
    Temp_1 = SubFun_CollectResults(SOLUTIONs{1,ii},[]);
    Temp_2 = SubFun_CollectResults(SOLUTIONs{2,ii},[]);
    KeyStat.AggC(:,ii)  =   [Temp_1.Eps_Eta_Y_H(3);Temp_2.Eps_Eta_Y_H(3)];   
    KeyStat.StdC(:,ii)  =   [Temp_1.Eps_Eta_Y_H(5);Temp_2.Eps_Eta_Y_H(5)];
    KeyStat.DiffC(:,ii) =   [Temp_1.Eps_Eta_Y_H(6);Temp_2.Eps_Eta_Y_H(6)];   
end
figure;
subplot(1,3,1);
plot(List_Elas,KeyStat.AggC);
subplot(1,3,2);
plot(List_Elas,KeyStat.StdC);
subplot(1,3,3);
plot(List_Elas,KeyStat.DiffC);